﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Items.Bosses.PumpkinBoy
{
    public class PumpkinBomb : ModItem
    {
        public override void SetStaticDefaults()
        {
            Item.ResearchUnlockCount = 99;
        }
        public override void SetDefaults()
        {
            Item.width = 40;
            Item.height = 40;
            Item.knockBack = 2f;
            Item.autoReuse = true;
            Item.maxStack = Item.CommonMaxStack;
            Item.value = Item.sellPrice(0, 0, 2, 12);
            Item.damage = 15;
            Item.noUseGraphic = true;
            Item.DamageType = DamageClass.Ranged;
            Item.consumable = true;
            Item.noMelee = true;
            Item.UseSound = SoundID.Item1;
            Item.useAnimation = 35;
            Item.useTime = 35;
            Item.useStyle = 1;
            Item.shoot = ModContent.ProjectileType<PumpkinBomb1>();
            Item.shootSpeed = 10f;
            Item.rare = ItemRarityID.Blue;
            Item.crit = 12;
        }
    }
    public class PumpkinBomb1 : ModProjectile
    {
        public override string Texture => "HardcoreGame/Content/Items/Bosses/PumpkinBoy/PumpkinBomb";
        public override void SetDefaults()
        {
            Projectile.width = 14;
            Projectile.height = 20;
            Projectile.DamageType = DamageClass.Ranged;
            Projectile.friendly = true;
            Projectile.hostile = false;
            Projectile.penetrate = -1;
            Projectile.aiStyle = 16;
            AIType = ProjectileID.BouncyGrenade;
        }
        public override Color? GetAlpha(Color lightColor)
        {
            return new Color?(Color.White * ((255 - Projectile.alpha) / 255f));
        }
        public override bool OnTileCollide(Vector2 oldVelocity)
        {
            Projectile.Kill();
            return false;
        }
        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            Projectile.Kill();
        }
        public override void AI()
        {
            if (Main.rand.NextBool(2))
            {
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height,
                    DustID.Pumpkin, 0f, 0f, 100, default, 0.8f);
            }
        }
        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item14, Projectile.Center);
            for (int i = 0; i < 20; i++)
            {
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height,
                    DustID.Torch, 0f, 0f, 100, Color.Orange, 1.2f);
            }
            if (Main.myPlayer == Projectile.owner)
            {
                Projectile projectile = Projectile.NewProjectileDirect(Projectile.GetSource_Death(), new Vector2(Projectile.Center.X, Projectile.Center.Y - 36f), new Vector2(0f, 0f), Main.rand.Next(694, 696), Projectile.damage / 3, 1f, Projectile.owner, 0f, 0f);
                projectile.rotation = 0f;
                projectile.scale = Utils.NextFloat(Main.rand, 0.5f, 1.5f);
                Projectile projectile2 = Projectile.NewProjectileDirect(Projectile.GetSource_Death(), new Vector2(Projectile.Center.X + 6f, Projectile.Center.Y - 30f), new Vector2(0f, 0f), Main.rand.Next(694, 696), Projectile.damage / 3, 1f, Projectile.owner, 0f, 0f);
                projectile2.rotation = 90f;
                projectile2.scale = Utils.NextFloat(Main.rand, 0.5f, 1.5f);
                Projectile projectile3 = Projectile.NewProjectileDirect(Projectile.GetSource_Death(), new Vector2(Projectile.Center.X, Projectile.Center.Y - 30f), new Vector2(0f, 0f), Main.rand.Next(694, 696), Projectile.damage / 3, 1f, Projectile.owner, 0f, 0f);
                projectile3.rotation = 180f;
                projectile3.scale = Utils.NextFloat(Main.rand, 0.5f, 1.5f);
                Projectile projectile4 = Projectile.NewProjectileDirect(Projectile.GetSource_Death(), new Vector2(Projectile.Center.X - 6f, Projectile.Center.Y - 36f), new Vector2(0f, 0f), Main.rand.Next(694, 696), Projectile.damage / 3, 1f, Projectile.owner, 0f, 0f);
                projectile4.rotation = 270f;
                projectile4.scale = Utils.NextFloat(Main.rand, 0.5f, 1.5f);
            }
            GeneratePumpkinGores(Projectile.Center, Projectile.width);
        }
        private void GeneratePumpkinGores(Vector2 position, int size)
        {
            for (int i = 0; i < 3; i++)
            {
                Vector2 gorePos = position + new Vector2(Main.rand.Next(-size / 2, size / 2), Main.rand.Next(-size / 2, size / 2));
                Vector2 goreVel = new(Main.rand.NextFloat(-3f, 3f), Main.rand.NextFloat(-3f, 3f));
                if (Main.netMode != NetmodeID.Server)
                {
                    Gore.NewGore(Projectile.GetSource_Death(), gorePos, goreVel, Mod.Find<ModGore>("Gore_1").Type, 1f);
                }
            }
        }
    }
}
